% Using clear mex, clear classes, or clear all, while a mexHID device is
% open causes problems, and may cause Matlab to hang or crash at the next
% mexHID('terminate').
%
% I use mexLock in my mexFunction implementation.  I'm not sure what else I
% can do to try to prevent this.  I assume Matlab is unloading memory
% without asking.  Maybe I can rely on mexMake*Persistent more atomically
% and implement a safe mexAtExit...
%
% Using plain old clear, with no options, is OK.  Using clear with one of
% the above options is OK, as long as you mexHID('terminate') *first*.

%%
% Not OK.
% Update: OK when using mexAtExit and not mexLock.
mexHID('terminate');
clear mexHID
clear classes
qm = dotsQueryableHIDMouse;
clear all
%clear mex
%clear classes
mexHID('terminate');

%%
% OK.
mexHID('terminate');
clear mexHID
clear classes
qm = dotsQueryableHIDMouse;
mexHID('terminate');
clear all
%clear mex
%clear classes
mexHID('terminate');

%%
% OK.
mexHID('terminate');
clear mexHID
clear classes
qm = dotsQueryableHIDMouse;
mexHID('terminate');
clear
mexHID('terminate');